package dmas.lab.schedulers;

import java.util.ArrayList;
import java.util.List;

public class Scheduler implements TaskCaller {

	List<Event> eventList;
	private Event currentEvent = null;

	public Scheduler() {
		eventList = new ArrayList<Event>();
	}

	public void addEvent(Event event) {
		synchronized (eventList) {
			if (eventList.isEmpty()) {
				eventList.add(event);
				currentEvent = event;
				event.run();
			}else{
				eventList.add(event);
				
			}
		}
	}

	public boolean deleteEvent(Event event) {
		return eventList.remove(event);
	}

	/***
	 * Handles information returned by Event
	 * */
	@Override
	public void callback(Object data) {
		synchronized (eventList) {
			System.out.println(data);
			if (currentEvent != null) {
				eventList.remove(currentEvent);
				if(!eventList.isEmpty()){
					currentEvent = eventList.get(0);
					currentEvent.run();
				}
			}
		}
	}

	@Override
	public void handleException(Exception e) {
		// Errory z eventow
		System.out.println(e.getMessage());
	}
}
